package com.runfast.waimai.utils;

import cn.jpush.api.push.PushResult;
import com.runfast.paotui.dao.model.Role;
import com.runfast.paotui.utils.JiGuangPushUtil;
import com.runfast.waimai.dao.model.RunfastLoginRecord;
import com.runfast.waimai.dao.model.RunfastLoginRecordExample;
import com.runfast.waimai.service.RunfastLoginRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: lijin
 * @date: 2018年04月13日
 */

@Async
@Component
@Slf4j
public class WaiMaiMessagePushTask {
    @Autowired
    private RunfastLoginRecordService runfastLoginRecordService;
   /* public void newOrderNotify(String orderNo, Integer agentNearById) {
        RunfastShopperExample shopperExample = new RunfastShopperExample();
        shopperExample.createCriteria().andAgentIdEqualTo(agentNearById);
        List<RunfastShopper> shoppers = waiMaiShopperService.selectByExample(shopperExample);

        HashMap<String, String> params = new HashMap<>();
        params.put("orderNo", orderNo);
        ArrayList<String> aliases = new ArrayList();
        for (RunfastShopper shopper : shoppers) {

            if (StringUtils.isNotBlank(shopper.getAlias()))
                aliases.add(shopper.getAlias());
        }
        if (aliases.isEmpty()) return;
        PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.DRIVER, 600, aliases, "您有新的跑腿订单" + orderNo, params, "neworder.caf");
        if (!pushResult.isResultOK()) {

            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            log.error("文件名：" + stackTraceElement.getFileName()
                    + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                    + " 行号：" + stackTraceElement.getLineNumber()
                    + " 描述：" + pushResult.getOriginalContent());
        }

    }*/

    /**
     * 推送客户
     * */
    public void takenOrderToCustomer(String orderNo, Integer cuserId,String message) {
        ArrayList<String> aliases = new ArrayList();
        RunfastLoginRecordExample example=new RunfastLoginRecordExample();
        example.createCriteria().andAccountidEqualTo(cuserId).andAccounttypeEqualTo(0).andLogouttimeIsNull();
        List<RunfastLoginRecord>  runfastLoginRecordList=runfastLoginRecordService.selectByExample(example);
        if(runfastLoginRecordList.size()>0){
             for(RunfastLoginRecord loginRecord:runfastLoginRecordList){
                  if(StringUtils.isNotBlank( loginRecord.getAlias())){
                      aliases.add(loginRecord.getAlias());
                  }
             }
        }
        if (aliases.size()>0) {
            HashMap<String, String> params = new HashMap<>();
            params.put("orderNo", orderNo);
            PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.CUSTOMER, 600, aliases, "您的外卖订单：" + orderNo + message, params, null);
            if (!pushResult.isResultOK()) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
                log.error("文件名：" + stackTraceElement.getFileName()
                        + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                        + " 行号：" + stackTraceElement.getLineNumber()
                        + " 描述：" + pushResult.getOriginalContent());
            }
        }
    }
    /**
     * 给商家推送
     * */
    public void takenOrderToBusiness(String orderNo, Integer businessId,String message) {
        ArrayList<String> aliases = new ArrayList();
        RunfastLoginRecordExample example=new RunfastLoginRecordExample();
        example.createCriteria().andAccountidEqualTo(businessId).andAccounttypeEqualTo(1).andLogouttimeIsNull();
        List<RunfastLoginRecord>  runfastLoginRecordList=runfastLoginRecordService.selectByExample(example);
        if(runfastLoginRecordList.size()>0){
            for(RunfastLoginRecord loginRecord:runfastLoginRecordList){
                if(StringUtils.isNotBlank( loginRecord.getAlias())){
                    aliases.add(loginRecord.getAlias());
                }
            }
        }
        if (aliases.size()>0) {
            HashMap<String, String> params = new HashMap<>();
            params.put("orderNo", orderNo);
            PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.BUSINESS, 600, aliases, "外卖订单：" + orderNo + message, params, null);
            if (!pushResult.isResultOK()) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
                log.error("文件名：" + stackTraceElement.getFileName()
                        + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                        + " 行号：" + stackTraceElement.getLineNumber()
                        + " 描述：" + pushResult.getOriginalContent());
            }
        }
    }


    /**给骑手推送*/
    public void takenOrderToShopper(String orderNo, Integer shopperId,String message) {
        ArrayList<String> aliases = new ArrayList();
        RunfastLoginRecordExample example=new RunfastLoginRecordExample();
        example.createCriteria().andAccountidEqualTo(shopperId).andAccounttypeEqualTo(2).andLogouttimeIsNull();
        List<RunfastLoginRecord>  runfastLoginRecordList=runfastLoginRecordService.selectByExample(example);
        if(runfastLoginRecordList.size()>0){
            for(RunfastLoginRecord loginRecord:runfastLoginRecordList){
                if(StringUtils.isNotBlank( loginRecord.getAlias())){
                    aliases.add(loginRecord.getAlias());
                }
            }
        }
        if (aliases.size()>0) {
            HashMap<String, String> params = new HashMap<>();
            params.put("orderNo", orderNo);
            PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.DRIVER, 600, aliases, "外卖订单：" + orderNo + message, params, null);
            if (!pushResult.isResultOK()) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
                log.error("文件名：" + stackTraceElement.getFileName()
                        + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                        + " 行号：" + stackTraceElement.getLineNumber()
                        + " 描述：" + pushResult.getOriginalContent());
            }
        }
    }

}
